package com.yc.projects.pro2_bootmail;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;



@Service
public class EmailService {
    Logger log=Logger.getLogger(EmailService.class.getName());

    @Autowired
    private JavaMailSender javaMailSender;

    public void sendEmail(String to, String subject, String text) {
        try {
            log.info("Preparing to send email to {}"+ to);
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom("2937053710@qq.com"); // 设置发件人
            message.setTo(to);
            message.setSubject(subject);
            message.setText(text);
            log.info("Sending email with subject: {}"+subject);
            javaMailSender.send(message);
            log.info("Email sent successfully to {}"+to);
        } catch (Exception e) {
            log.error("Failed to send email to {}"+e);
        }
    }
}
